/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.cncqs.backend.applet.service;

import com.jeesite.modules.file.entity.FileEntity;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.cncqs.backend.applet.entity.AppletFoods;
import com.cncqs.backend.applet.dao.AppletFoodsDao;

import java.util.List;

/**
 * 食品表Service
 * @author cxy
 * @version 2019-05-05
 */
@Service
@Transactional(readOnly=true)
public class AppletFoodsService extends CrudService<AppletFoodsDao, AppletFoods> {

	@Value("${imageBaseUrl}")
	private String imageBaseUrl;
	
	/**
	 * 获取单条数据
	 * @param appletFoods
	 * @return
	 */
	@Override
	public AppletFoods get(AppletFoods appletFoods) {
		return super.get(appletFoods);
	}
	
	/**
	 * 查询分页数据
	 * @param appletFoods 查询条件
	 * @return
	 */
	@Override
	public Page<AppletFoods> findPage(AppletFoods appletFoods) {
		Page<AppletFoods> page = super.findPage(appletFoods);
		List<AppletFoods> list = page.getList();
		for (AppletFoods entity : list) {
			List<FileUpload> businessPictureUrl = FileUploadUtils.findFileUpload(entity.getId(), "picture");
			if (businessPictureUrl != null && businessPictureUrl.size() > 0) {
				FileEntity fileEntity = businessPictureUrl.get(0).getFileEntity();
				entity.setPicture(imageBaseUrl + fileEntity.getFilePath() + fileEntity.getFileId() + "." + fileEntity.getFileExtension());
			}
		}
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param appletFoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(AppletFoods appletFoods) {
		FileUploadUtils.saveFileUpload(appletFoods.getId(), "picture");
		super.save(appletFoods);
	}
	
	/**
	 * 更新状态
	 * @param appletFoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(AppletFoods appletFoods) {
		super.updateStatus(appletFoods);
	}
	
	/**
	 * 删除数据
	 * @param appletFoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(AppletFoods appletFoods) {
		super.delete(appletFoods);
	}
	
}